(function($, owner) {
	/**
	 * 用户登录
	 **/
	owner.login = function(loginInfo, callback) {
		var wt=plus.nativeUI.showWaiting();
		callback = callback || $.noop;
		if(loginInfo==null){//参数为空则为自动登陆
			loginInfo = JSON.parse(localStorage.getItem('users') || '[]');
		}
		else{
			loginInfo = loginInfo || {};
			loginInfo.name = loginInfo.name || '';
			loginInfo.passwd = loginInfo.passwd || '';
			if (loginInfo.name.length <2) {
				wt.close();
				return callback('用户名不低于3个字符！');
			}
			if (loginInfo.passwd.length < 6 && loginInfo.passwd.length!=0) {//第三方登录密码为空
				wt.close();
				return callback('密码最短为 6 个字符');
			}	
		}
		$.ajax({
				url:"http://118.24.14.224/carStation/login.php",
				data:loginInfo,
				async: true,
				type:'post',
				dataType:'text',
				timeout:10000,
				success:function(data){
					if(data==0 || data=="0"){
						wt.close();
						return callback('用户名或密码错误');
					}
					localStorage.setItem('uid',data);
					if(owner.getSettings().autoLogin){//如果是自动登录，则把用户名密码存储起来
						localStorage.setItem('users', JSON.stringify(loginInfo));
					}
					wt.close();
					return owner.createState(loginInfo.account, callback);
				},
				error:function(xhr,type,errorThrown){
					wt.close();
					return callback('用户名或密码错误');
				}
		});
	};

	owner.createState = function(name, callback) {
		var state = owner.getState();
		state.account = name;
		state.token = "token123456789";
		owner.setState(state);
		return callback();
	};

	/**
	 * 新用户注册
	 **/
	owner.reg = function(regInfo, callback) {
		callback = callback || $.noop;
		regInfo = regInfo || {};
		regInfo.mobile = regInfo.mobile || '';
		regInfo.password = regInfo.password || '';
		if (regInfo.mobile.length !=11 ) {
			return callback('手机号格式不正确');
		}
		if (regInfo.passwd.length < 6) {
			return callback('密码最短需要 6 个字符');
		}
		$.ajax({
			url:"http://118.24.14.224/carStation/register.php",
			data:regInfo,
			async: true,
			type:'post',
			dataType:'json',
			timeout:10000,
			success:function(data){
				var users = JSON.parse(localStorage.getItem('$users') || '[]');
				users.push(regInfo);
				localStorage.setItem('$users', JSON.stringify(users));
				return callback();
			},
			error:function(xhr,type,errorThrown){
				return callback(type);
			}
		});
	};

	/**
	 * 获取当前状态
	 **/
	owner.getState = function() {
		var stateText = localStorage.getItem('$state') || "{}";
		return JSON.parse(stateText);
	};

	/**
	 * 设置当前状态
	 **/
	owner.setState = function(state) {
		state = state || {};
		localStorage.setItem('$state', JSON.stringify(state));
		//var settings = owner.getSettings();
		//settings.gestures = '';
		//owner.setSettings(settings);
	};

	var checkEmail = function(email) {
		email = email || '';
		return (email.length > 3 && email.indexOf('@') > -1);
	};

	/**
	 * 找回密码
	 **/
	owner.forgetPassword = function(email, callback) {
		callback = callback || $.noop;
		if (!checkEmail(email)) {
			return callback('邮箱地址不合法');
		}
		return callback(null, '新的随机密码已经发送到您的邮箱，请查收邮件。');
	};

	/**
	 * 获取应用本地配置
	 **/
	owner.setSettings = function(settings) {
		settings = settings || {};
		localStorage.setItem('$settings', JSON.stringify(settings));
	}

	/**
	 * 设置应用本地配置
	 **/
	owner.getSettings = function() {
			var settingsText = localStorage.getItem('$settings') || "{}";
			return JSON.parse(settingsText);
		}
		/**
		 * 获取本地是否安装客户端
		 **/
	owner.isInstalled = function(id) {
		if (id === 'qihoo' && mui.os.plus) {
			return true;
		}
		if (mui.os.android) {
			var main = plus.android.runtimeMainActivity();
			var packageManager = main.getPackageManager();
			var PackageManager = plus.android.importClass(packageManager)
			var packageName = {
				"qq": "com.tencent.mobileqq",
				"weixin": "com.tencent.mm",
				"sinaweibo": "com.sina.weibo"
			}
			try {
				return packageManager.getPackageInfo(packageName[id], PackageManager.GET_ACTIVITIES);
			} catch (e) {}
		} else {
			switch (id) {
				case "qq":
					var TencentOAuth = plus.ios.import("TencentOAuth");
					return TencentOAuth.iphoneQQInstalled();
				case "weixin":
					var WXApi = plus.ios.import("WXApi");
					return WXApi.isWXAppInstalled()
				case "sinaweibo":
					var SinaAPI = plus.ios.import("WeiboSDK");
					return SinaAPI.isWeiboAppInstalled()
				default:
					break;
			}
		}
	}
}(mui, window.app = {}));